package demo.database.H2.D04_导出导入;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.titan.common.v8.interfaces.IEmpty;
import com.titan.core.utils.db.DataSourceUtils;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.file.FileUtils;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.utils.spring.SpringUtils;
import com.titan.core.web.utils.SpringBootUtils;
import com.titan.services.Serialize.v1.SerializeUtils;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2ControlConfigDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2ControlDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2PageConfigDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2PageDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2SystemConfigDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2SystemDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2ThemeConfigDaoImpl;
import com.titan.services.ddm.v2.common.DaoImpl.Ddm2ThemeDaoImpl;
import com.titan.services.ddm.v2.common.domain.Ddm2ThemeDomain;
import com.titan.services.ddm.v2.models.Ddm2Utils;

public class D04_导出到序列化文件 {
    private static void run() throws Exception {
        String token = Ddm2Utils.TOKEN_DEFAULT;
        String url = "jdbc:h2:file:E:/HDJT/S1502/S1502"; 
        String username = "root";
        String password = "S1502@hdjt";
        
        DataSource dataSource = DataSourceUtils.registerH2dbDataSource(IEmpty.EMPTY_ID, url, username, password, 5, 5);
        try {
            JdbcUtils.setDataSource(dataSource);
            File file = new File("E:\\"+Ddm2ThemeDomain.generateTablename());

//            IDdm2ThemeDao dao = SpringUtils.getBean(IDdm2ThemeDao.class);
//            List<Ddm2ThemeDomain> items = dao.findByWhere(token, Ddm2ThemeWhere.$());
//            ConsoleUtils.log("size", items.size());
//            byte[] bytes = SerializeUtils.toBytes(items);
//            ConsoleUtils.log("bytes.length", bytes.length);
//            FileUtils.write(file, bytes, false);
            
//            byte[] bytes = FileUtils.toByteArray(file);
//            List<Ddm2ThemeDomain> items = SerializeUtils.toObject(bytes);
//            for (int i = 0; i < items.size(); i++) {
//                Ddm2ThemeDomain domain = items.get(i);
//                ConsoleUtils.log("i", i, "getTheme_name", domain.getTheme_name());
//            }
            
//            List<Map<String,Object>> items = JdbcUtils.queryForListInMap("select * from ddm2_theme");
//            ConsoleUtils.log("size", items.size());
//            for (int i = 0; i < items.size(); i++) {
//                ConsoleUtils.log(items.get(i));
//            }
//            byte[] bytes = SerializeUtils.toBytes(items);
//            ConsoleUtils.log("bytes.length", bytes.length);
//            FileUtils.write(file, bytes, false);
            
//            byte[] bytes = FileUtils.toByteArray(file);
//            List<Map<String,Object>> items = SerializeUtils.toObject(bytes);
//            for (int i = 0; i < items.size(); i++) {
//                ConsoleUtils.log("i", i, "theme_name", items.get(i).get("Theme_name"));
//            }
        } finally {
            DataSourceUtils.close(dataSource);
        }
        
    }


    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            SpringBootUtils.initEnvironmentTitan(args);
            
            SpringUtils.register(Ddm2SystemDaoImpl.class);
            SpringUtils.register(Ddm2SystemConfigDaoImpl.class);
            SpringUtils.register(Ddm2ThemeDaoImpl.class);
            SpringUtils.register(Ddm2ThemeConfigDaoImpl.class);
            SpringUtils.register(Ddm2PageDaoImpl.class);
            SpringUtils.register(Ddm2PageConfigDaoImpl.class);
            SpringUtils.register(Ddm2ControlDaoImpl.class);
            SpringUtils.register(Ddm2ControlConfigDaoImpl.class);
            
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print();
        }
    }
}
